package com.example.demo.controller;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.example.demo.dto.goodsdto.GoodsDto;
import com.example.demo.error.Result;
import com.example.demo.error.ResultGenerator;
import com.example.demo.exception.BusinessException;
import com.example.demo.pojo.InventoryVO;
import com.example.demo.service.GoodsService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/pay")
/**
 * 商品查询Controller
 */
public class PayController {
    @Autowired
    private Config config;

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/facetoface")
    public Result<?> faceToFace() throws Exception {
        Factory.setOptions(config);
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate("iphone15 proMax 1T", "1213731317137", "12999");
        String httpBody = response.getHttpBody();
        //转换为Json
        log.info("httpBody:{}", httpBody);
        if (StrUtil.isBlank(httpBody)) {
            throw new BusinessException(500,"支付出现异常");
        }
        JSONObject jsonObject = JSONUtil.parseObj(httpBody);
        String qcUrl = jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
        BufferedImage image = QrCodeUtil.generate(qcUrl, 300, 300);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", outputStream);
        byte[] byteArray = outputStream.toByteArray();
        String encode = Base64.encode(byteArray);
        return ResultGenerator.genSuccessfulResult("data:image/png;base64," + encode,"生成支付二维码成功");
    }

    @PostMapping("/notify")
    public Result<?> notify(HttpServletRequest request) throws Exception {
        log.info("收到支付通知");
        String outTradeNo = request.getParameter("out_trade_no");
        log.info("流水号：{}",outTradeNo);
        return ResultGenerator.genSuccessResult();
        //
    }

    //查询
    @GetMapping("/select/goods")
    public Result<?> selectGoods(@RequestParam Map<String, Object> params) throws ParseException {
        PageInfo<InventoryVO> goodsList = goodsService.selectGoods(params);
        Map<String,Object> map = MapUtil.newHashMap();
        map.put("total",goodsList.getTotal());
        map.put("pages",goodsList.getPages());
        map.put("pageNum",goodsList.getPageNum());
        map.put("pageSize",goodsList.getPageSize());
        map.put("list",goodsList.getList());
       return ResultGenerator.genSuccessResult(map);
    }


}
